package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.wemedia.entity.WmNewsMaterial;
import com.heima.wemedia.mapper.WmNewsMaterialMapper;
import com.heima.wemedia.service.WmNewsMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 自媒体图文引用素材信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2022-04-27
 */
@Service
public class WmNewsMaterialServiceImpl extends ServiceImpl<WmNewsMaterialMapper, WmNewsMaterial> implements WmNewsMaterialService {

    /**
     * 根据文章id删除中间表数据
     *
     * @param id
     */
    public void removeNewsId(Integer id) {

        LambdaQueryWrapper<WmNewsMaterial> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(WmNewsMaterial::getNewsId,id);
        remove(queryWrapper);
    }


    /**
     * 批量保存文章素材数据
     *
     * @param id
     * @param materialIds
     * @param i
     */
    public void saveNewMaterialBatch(Integer id, List<Integer> materialIds, int i) {

        List<WmNewsMaterial> materialList = new ArrayList<>();
        // 循环保存数据
        for (Integer materialId : materialIds) {
            WmNewsMaterial wmNewsMaterial = new WmNewsMaterial();
            wmNewsMaterial.setType(i);
            wmNewsMaterial.setNewsId(id);
            wmNewsMaterial.setMaterialId(materialId);
            materialList.add(wmNewsMaterial);
        }

        saveBatch(materialList);
    }
}
